import Vue from 'vue'
import VueRouter from 'vue-router'
// 引入Vuex
import store from '@/store'

const VueRouterPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(to) {
	return VueRouterPush.call(this, to).catch(() => { })
}

const Login = () => import('../views/Login/index.vue')
const layout = () => import('../layout/index.vue')
const Home = () => import('../views/Home/index.vue')
const AddAdmin = () => import('../views/AddAdmin/index.vue')
const QueryAdmin = () => import('../views/QueryAdmin/index.vue')
const Video = () => import('../views/Video/index.vue')
const Menber = () => import('../views/Menber/index.vue')
const Order = () => import('../views/Order/index.vue')
const Log = () => import('../views/Log/index.vue')
const Personage = () => import('../views/Personage/index.vue')
const Classifyr = () => import('../views/Classifyr/index.vue')


Vue.use(VueRouter)

const routes = [
	{
		path: '/',
		redirect: '/home',
		component: layout,
		children: [
			{
				path: 'home',
				component: Home,
				meta: { title: '首页' },
			}
		]
	},
	{
		path: '/addadmin',
		component: layout,
		children: [
			{
				path: '',
				component: AddAdmin,
				meta: { title: '增加管理员信息' },
			}
		]
	},
	{
		path: '/queryadmin',
		component: layout,
		children: [
			{
				path: '',
				component: QueryAdmin,
				meta: { title: '查询管理员信息' },
			}
		]
	},
	{
		path: '/classifyr',
		component: layout,
		children: [
			{
				path: '',
				component: Classifyr,
				meta: { title: '大标题管理' },
			}
		]
	},
	{
		path: '/video',
		component: layout,
		children: [
			{
				path: '',
				component: Video,
				meta: { title: '视频管理' },
			}
		]
	},
	{
		path: '/menber',
		component: layout,
		children: [
			{
				path: '',
				component: Menber,
				meta: { title: '会员管理' },
			}
		]
	},
	{
		path: '/order',
		component: layout,
		children: [
			{
				path: '',
				meta: { title: '支付宝订单信息' },
				component: Order,
			}
		]
	},
	{
		path: '/log',
		component: layout,
		children: [
			{
				path: '',
				component: Log,
				meta: { title: '登陆日志' },
			}
		]
	},
	{
		path: '/personage',
		component: layout,
		children: [
			{
				path: '',
				component: Personage,
				meta: { title: '个人信息' },
			}
		]
	},
	{
		path: '/Login',
		name: 'login',
		meta: { title: '后台管理系统登陆' },
		component: Login
	}
]

const router = new VueRouter({
	// mode: 'history',
	base: process.env.BASE_URL,
	routes
})
// 全局前置路由守卫--在每次被切换前调用--初始化被调用
router.beforeEach(async (to, from, next) => {
	// 没有token
	let token = undefined;
	if (store.state.token) {
		token = store.state.token
	} else {
		token = localStorage.getItem('tt')
		store.commit('getToken', token)
	}

	// 有token情况
	if (!token) {
		if (to.path == '/login') {
			next()
		} else {
			next('/login')
		}
	} else {
		// 判断Vuex中是否存在用户信息

		// 当前用户信息存在情况
		if (!store.state.getUser.username) {
			// 判断token真伪
			try {
				await store.dispatch('getUserProfile')
				if (to.path == '/login') {
					next('/')
				} else {
					next()
				}
			} catch (error) {
				localStorage.removeItem('tt');
				store.commit('saveToken', '');
				if (to.path == '/login') {
					next()
				} else {
					next('/login');
				}
			}

		} else {
			if (to.path == '/Login') {
				next('/')
			} else {
				next()
			}
		}

	}
})

// 全局后置路由守卫--在每次被切换后调用
router.afterEach((to, from) => {
	document.title = to.meta.title
})

export default router
